import cn.yunhe.model.Books;
import cn.yunhe.model.Orders;
import cn.yunhe.model.Users;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.List;


public class Demo2 {
    Session session = null;

    @Before
    public void before() {
        Configuration config = new Configuration().configure();
        SessionFactory factory = config.buildSessionFactory();
        session = factory.openSession();
    }

    @Test
    public void test1() {
        List<Books> books = session.createQuery("from Books").list();
        for (Books b : books) {
            System.out.println(b.getTitle());
        }
    }

    @Test
    public void test2() {
        Query query = session.createQuery("select o.users from Orders o where o.id=1");
        List<Users> list = query.list();
        System.out.println(list.get(0).getName());

    }

    @Test
    public void test3() {
        Query query = session.createQuery("select u from Users u,Orders o where u=o.users and o.id=1");
        List<Users> list = query.list();
        for (Users u : list
        ) {
            System.out.println(u.getName());
        }
    }

    @Test
    public void test4() {
        List<Users> list = session.createQuery("select u from Users u where u in(select o.users from Orders o)").list();
        for (Users u : list
        ) {
            System.out.println(u.getName());
        }
    }

    @Test
    public void test5() {
        List<Users> list = session.createQuery("select distinct u from Users u inner join u.orderses").list();
        for (Users u : list) {
            System.out.println(u.getName() + "的订单");
            for (Orders o : u.getOrderses()) {
                System.out.println(o.getId() + "\t" + o.getTotalprice());
            }
        }
    }

    @Test
    public void test6() {
        List<Users> list = session.createQuery("select u from Users u left join u.orderses o where o is null ").list();
        for (Users u : list) {
            System.out.println(u.getName());
        }
    }

    @After
    public void after() {
        session.close();
    }
}
